package com.study.springmvc.v9mvc.config;

import com.study.springmvc.v9mvc.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.SpringServletContainerInitializer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = {"com.study.springmvc.v9mvc.controller", "com.study.springmvc.v9mvc.service"})
@EnableWebMvc
@EnableAsync
public class MvcConfig implements WebMvcConfigurer {
    //@EnableWebMvc 等于开启SpringMVC注解方式
    //@Configuration xml
    // @ComponentScan("com.mayikt.controller") mvc 扫包范围
    //DispatcherServlet

    //SpringServletContainerInitializer

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    //1、 注入 Interceptor Bean
    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

    //2. 添加Interceptor 拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor()).addPathPatterns("/**");  //拦截所有请求
    }
}
